package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import dao.FlowerDao;
import entity.Flower;
import util.DBUtils;

public class FlowerDaoImpl implements FlowerDao {

	@Override
	public List<Flower> selectByPage(int p) {
		try(Connection c = DBUtils.getConnection()){
			String sql = "select * from flower limit ?,?";
			PreparedStatement ps = c.prepareStatement(sql);
			ps.setInt(1, (p-1)*15);
			ps.setInt(2, p*15);
			ResultSet rs = ps.executeQuery();
			List<Flower> fs = new ArrayList<>();
			while (rs.next()) {
				Flower f = new Flower();
				f.setCode(rs.getInt(1));
				f.setName(rs.getString(2));
				f.setPrice(rs.getBigDecimal(3));
				f.setCount(rs.getInt(4));
				f.setInfo(rs.getString(5));
				f.setDetail(rs.getString(6));
				f.setScore(rs.getInt(7));
				fs.add(f);
			}
			return fs.isEmpty() ? Collections.emptyList() : fs;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return Collections.emptyList();
	}

}
